package com.gjun.struts.resource.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gjun.struts.bean.CategoryJsonBean;
import com.gjun.struts.bean.ext.Book;
import com.gjun.struts.bean.ext.Category;
import com.gjun.struts.bean.ext.Product;
import com.gjun.struts.resource.dao.ICategoryDao;


public class CategoryDaoImpl extends HibernateDaoSupport implements ICategoryDao {
	

	public List<Category> findAllCategory() {
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql =  "select distinct cat from Category cat left outer join fetch cat.subCats where cat.parentId=1";
		Query query = session.createQuery(hql);
		List<Category> list = query.list();
		session.close();
		return list;
	}


	public Set<Product> findAllProductByCategory(int categoryId) {
		System.out.println("categoryId-----------------------------------------"+categoryId);
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql = "from Category c where c.id=:id";
		session.beginTransaction().begin();
		Query query = session.createQuery(hql);
		query.setParameter("id", categoryId);
		Category category = (Category)query.uniqueResult();
		Set<Product> products = category.getProducts();
		session.close();
		
		return products;
	}



	public int getTotalCotegoryNum() {
		int num = 0;
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql =  "select distinct cat from Category cat left outer join fetch cat.subCats where cat.parentId=1";
		Query query = session.createQuery(hql);
		List<Category> cates = query.list();
		for (Category category : cates) {
			Set<Product> products = category.getProducts();
			for (Product product : products) {
				num = num+1;
			}
		}
		session.close();
		return num;
	}


	//
	public List<Category> findAllJXDNovelCagegory() {
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql =  "select distinct cat from Category cat left outer join fetch cat.subCats where cat.parentId=10";
		Query query = session.createQuery(hql);
		List<Category> list = query.list();
		session.close();
		return list;

	}


	public List<Book> findAllProductByCategoryNo(int categoryId,int pageNo,int pageSize) {
		List<Book> books = new ArrayList<Book>();
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		String hql = "from Category c where c.parentId=:parentId order by id";
		Query query = session.createQuery(hql);
		query.setParameter("parentId", categoryId);
		List<Category> categories = query.list();
		for (Category category : categories) {
			Set<Product> products = category.getProducts();
			for (Product product : products) {
				Book book = (Book)product;
				books.add(book);
			}
		}
		session.close();
		return books;
	}


//	public List<CategoryJsonBean> findAllCategoryJsonBean() {
//
//		return (List<CategoryJsonBean>) this.getHibernateTemplate().execute(new HibernateCallback() {
//			
//			public Object doInHibernate(Session session) throws HibernateException,
//					SQLException {
//				Query query = session.createQuery("from Category c where c.parentId=1");
//				List<Category> list = null;
//				try{
//					 list = query.list();
//					 for (Category category : list) {
//						
//					}
//				} catch (Exception e) {
//					// TODO: handle exception
//					e.printStackTrace();
//				}
//				return list;
//			}
//		});
//		
//		
//	}
	

	
}
